<template>
  <div>
    <Row>
      <i-col span="12" push="1">
        <Form :model="reviseUserInfo" :label-width="120" ref="reviseUserInfo" :rules="reviseUserInfoRule">

          <Form-item label="登陆密码" prop="loginPwd">
            <Input type="password" autocomplete="off" v-model="reviseUserInfo.loginPwd" placeholder="请输入登陆密码"></Input>
          </Form-item>
          <Form-item label="手机号" props="mobile">
            <Input v-model="reviseUserInfo.mobile" placeholder="请输入手机号"></Input>
          </Form-item>
          <FormItem label="验证码" prop="verificationCode">
            <Row>
              <i-col :lg="14">
                <Input v-model="reviseUserInfo.verificationCode" placeholder="请输入验证码"></Input>
              </i-col>
              <i-col :lg="8" offset="2">
                <Button type="primary" v-if="isSend === 0" @click="sendVerificationCode">
                  {{reviseUserInfo.send}}
                </Button>
                <Button type="primary" disabled v-if="isSend === 1">{{second}}</Button>
              </i-col>
            </Row>
          </FormItem>
          <Form-item label="真实名" props="realName">
            <Input v-model="reviseUserInfo.realName" placeholder="请输入真实名"></Input>
          </Form-item>
          <Form-item label="邮箱" props="email">
            <Input v-model="reviseUserInfo.email" placeholder="请输入邮箱"></Input>
          </Form-item>
          <Form-item label="职位" props="position">
            <Input v-model="reviseUserInfo.position" placeholder="请输入职位"></Input>
          </Form-item>

          <FormItem label="企业头像" prop="voucher">
            <div>
              <div class="demo-upload-list" v-for="item in uploadList">
                <img :src="item.url">
                <div class="demo-upload-list-cover">
                  <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
                </div>
              </div>
              <Upload ref="upload"
                      :show-upload-list="false"
                      :format="['jpg','jpeg','png','bmp']"
                      :max-size="1024"
                      :before-upload="handleBeforeUpload"
                      :on-format-error="handleFormatError"
                      :on-exceeded-size="handleMaxSize"
                      accept='image/*'
                      multiple
                      type="drag"
                      action="//jsonplaceholder.typicode.com/posts/"
                      style="display: inline-block;width:58px;">
                <div style="width: 58px;height:58px;line-height:70px;text-align: center;">
                  <Icon type="ios-cloud-upload-outline" size="30"></Icon>
                </div>
              </Upload>
              <div style="font-family: PingFangSC-Regular;font-size: 12px;color: #AAAAAA;letter-spacing: 0;">只能上传
                jpg/png 文件，且不超过 1 MB
              </div>
            </div>
          </FormItem>
          <Form-item>
            <Button type="primary" @click="submitChange">提交</Button>
            <Button type="ghost" style="margin-left: 8px" @click="resetFram">取消</Button>
          </Form-item>
        </Form>
      </i-col>
    </Row>
  </div>
</template>
<script>
  import {mapGetters} from 'vuex'
  import {sendSms} from '@api/common'
  import {updateUserInfo} from '@api/common/user'

  export default {
    name: 'trackingCode',
    data () {
      let uploadList = []
      let second = 60
      let isSend = 0
      let reviseUserInfo = {
        mobile: '',
        verificationCode: '',
        email: '',
        legaler: '',
        send: '获取验证码'
      }
      let reviseUserInfoRule = {}
      let datas = {uploadList, second, isSend, reviseUserInfo, reviseUserInfoRule}
      return datas
    },
    methods: {
      handleBeforeUpload (file) {
        let reader = new FileReader()
        reader.readAsDataURL(file)
        this.uploadList = []
        const _this = this
        reader.onloadend = function (e) {
          if (file.size <= 1024 * 1024) {
            if (file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/bmp') {
              file.url = reader.result
              _this.uploadList.push(file)
            }
          }
        }
        return false
      },
      handleRemove (file) {
        // this.uploadList.splice(this.uploadList.indexOf(file), 1)
        this.uploadList = []
      },
      handleFormatError (file) {
        this.$Notice.warning({
          title: '文件格式不正确',
          desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
        })
      },
      handleMaxSize (file) {
        this.$Notice.warning({
          title: '超出文件大小限制',
          desc: '文件 ' + file.name + ' 太大，不能超过 1M。'
        })
      },
      sendVerificationCode () {
        if (!this.reviseUserInfo.mobile) {
          this.$Message.error('手机号必填!')
          return
        }
        let myreg = /^[1][3,4,5,7,8][0-9]{9}$/
        if (!myreg.test(this.reviseUserInfo.mobile)) {
          this.$Message.error('手机号码有误!')
          return
        }
        let mobilePhone = this.reviseUserInfo.mobile
        let type = 'updateInfo'
        let param = {mobilePhone, type}
        sendSms(param).then(res => {
          if (+res.data.isSuccess === 1) {
            this.$Message.success('发送成功!')
            this.isSend = 1
            this.jian()
          } else {
            this.$Message.error(res.data.res)
          }
        })
      },
      jian () {
        let that = this
        setTimeout(function () {
          that.second = that.second - 1
          if (that.second === 0) {
            that.is_send = 0
            that.second = 60
          } else {
            that.jian()
          }
        }, '1000')
      },
      submitChange () {
        let formData = new FormData()
        if (!this.reviseUserInfo.loginPwd) {
          this.$Message.error('登陆密码必填!')
          return false
        }
        formData.append('loginPwd', this.reviseUserInfo.loginPwd)
        if (this.reviseUserInfo.mobile) {
          let myreg = /^[1][3,4,5,7,8][0-9]{9}$/
          if (!myreg.test(this.reviseUserInfo.mobile)) {
            this.$Message.error('手机号码有误!')
            return
          }
          formData.append('mobilePhone', this.reviseUserInfo.mobile)
          if (!this.reviseUserInfo.verificationCode) {
            this.$Message.error('验证码必填!')
            return false
          }
          formData.append('verifyCode', this.reviseUserInfo.verificationCode)
        }
        if (this.reviseUserInfo.realName) {
          formData.append('realName', this.reviseUserInfo.realName)
        }
        if (this.reviseUserInfo.position) {
          formData.append('position', this.reviseUserInfo.position)
        }
        if (this.reviseUserInfo.email) {
          formData.append('email', this.reviseUserInfo.email)
        }
        if (this.uploadList && this.uploadList.length > 0) {
          this.uploadList.forEach((file) => {
            formData.append('img', file)
          })
        }
        // if ((this.reviseUserInfo.mobile && this.reviseUserInfo.verificationCode) || this.reviseUserInfo.realName || (this.uploadList && this.uploadList.length > 0)) {
        updateUserInfo(formData).then((res) => {
          if (res.data.isSuccess === 1) {
            this.$Message.success('修改成功，重新登陆即可生效!')
            this.reviseUserInfo = {
              mobile: '',
              verificationCode: '',
              realName: '',
              send: '获取验证码',
              loginPwd: ''
            }
            this.uploadList = []
          } else {
            this.$Message.error(res.data.res)
          }
        })
        // } else {
        //   this.$Message.error('请添加修改信息后提交！')
        //   return false
        // }
      },
      resetFram () {
        this.reviseUserInfo = {
          mobile: '',
          verificationCode: '',
          realName: '',
          send: '获取验证码',
          loginPwd: ''
        }
        this.uploadList = []
      }
    },
    computed: {
      ...mapGetters(['currentUser'])
    },
    mounted () {
      this.reviseUserInfo = this.currentUser
      console.log(this.currentUser)
    }
  }
</script>
<style>
  .demo-upload-list {
    display: inline-block;
    width: 60px;
    height: 60px;
    text-align: center;
    line-height: 60px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
    margin-right: 4px;
  }

  .demo-upload-list img {
    width: 100%;
    height: 100%;
  }

  .demo-upload-list-cover {
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, .6);
  }

  .demo-upload-list:hover .demo-upload-list-cover {
    display: block;
  }

  .demo-upload-list-cover i {
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }
</style>
